package com.sdry.good.goodWarehouse.controller;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import com.google.common.reflect.TypeToken;
import com.google.gson.Gson;
import com.sdry.basic.entity.*;
import com.sdry.basic.service.ContainerService;
import com.sdry.basic.service.ContainerStareService;
import com.sdry.basic.service.WarehousesRegionLocationService;
import com.sdry.common.BaseAPIMessageTypeEnum;
import com.sdry.common.ContainerTypeEnum;
import com.sdry.good.goodChain.entity.GoodChain;
import com.sdry.good.goodWarehouse.entity.GoodContainerStare;
import com.sdry.good.goodWarehouse.service.GoodContainerStareService;
import com.sdry.good.logistics.entity.ReportPicking;
import com.sdry.good.logistics.service.ReportPickingService;
import com.sdry.good.out.entity.OutStockOrder;
import com.sdry.good.out.entity.OutStockOrderDetail;
import com.sdry.good.out.service.OutStockOrderDetailService;
import com.sdry.good.out.service.OutStockOrderService;
import com.sdry.product.prod.entity.LacquerInto;
import com.sdry.product.prod.entity.ResultAppList;
import com.sdry.product.prod.service.LacquerIntoService;
import com.sdry.product.waitingarea.entity.StayContainerStare;
import com.sdry.system.entity.SysUser;
import com.sdry.system.mapper.UserMapper;
import com.sdry.transit.entity.TransitOrderDetail;
import com.sdry.untils.DateUtil;
import com.sdry.untils.ResponseUtil;
import org.apache.poi.util.StringUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.ui.Model;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;
import java.io.PrintWriter;
import java.util.Date;
import java.util.stream.Collectors;

import com.sdry.good.goodWarehouse.entity.GoodSortFrame;
import com.sdry.good.goodWarehouse.service.GoodSortFrameService;
import com.sdry.good.goodWarehouse.mapper.GoodSortFrameMapper;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

/**
 *
 *@ClassName: GoodSortFrameController
 *@Description: 成品上架表
 *@Author tdd
 *@Date 2020-03-25 16:05:59
 *@version 1.0
*/
@Controller
@RequestMapping("/goodSortFrame")
public class GoodSortFrameController{
	@Resource GoodSortFrameService goodSortFrameService;
	@Resource
	GoodContainerStareService goodContainerStareService;
	@Resource
	ContainerService containerService;
	@Resource
	LacquerIntoService lacquerIntoService;
	@Resource
	WarehousesRegionLocationService warehousesRegionLocationService;
	@Resource
	ContainerStareService containerStareService;
	@Resource
	OutStockOrderService outStockOrderService;
	@Resource
	OutStockOrderDetailService outStockOrderDetailService;
	@Resource
	ReportPickingService reportPickingService;
	@Resource
	private UserMapper userMapper;
	//声明一个Logger
	private final static Logger logger = LoggerFactory.getLogger(GoodSortFrameController.class);
	/** 
	 * 进入页面
	 * @param model
	 * @return 页面路径
	*/
	@RequestMapping("/page")
	public String pageGoodSortFrame (Model model) {
		return "/td/goodWarehouse/goodSortFrame";
	}
/**
	 * 进入页面
	 * @param model
	 * @return 页面路径
	*/
	@RequestMapping("/pageGoodBinding")
	public String pageGoodBinding (Model model) {
		return "/td/goodWarehouse/goodBinding";
	}

	/** 
	 * 根据条件分页查询
	 * @param param 实体条件
	 * @param response
	*/
	@RequestMapping("/list")
	public void list(GoodSortFrame param,HttpServletResponse response){
		logger.info("根据条件分页查询条件"+JSONArray.fromObject(param));
		/*
		 * 条件分页查询内容
		 */
		List<GoodSortFrame> list = goodSortFrameService.findPageByMution(param);
		logger.info("根据条件分页查询条件返回内容"+JSONArray.fromObject(list));
		/*
		 * 条件查询总条数
		 */
		Integer total = goodSortFrameService.selectCountByMution(param);
		logger.info("条件查询总条数返回条数"+total);
		/*
		 * 转为json数据传至页面
		 */
		//创建result
		JSONObject jsonObject = new JSONObject();
		//添加入jsonObject
		jsonObject.put("code", 0);
		jsonObject.put("rows", list);
		jsonObject.put("total", total);
		try {
			ResponseUtil.write(response, jsonObject);
		} catch (Exception e) {
			logger.info("根据条件分页查询返回数据失败");
			e.printStackTrace();
		}
	}
	/****************************************app*******************************************/
	/**
	 * app根据编号查询容器信息
	 * @param cnumber 框码
	 * @param permissionType 产线/库区编号
	 * @param response
	 * @return
	 */
	@RequestMapping("/api/findContainerByCnumber")
	public void findContainerByCnumber(String cnumber,String permissionType,HttpServletResponse response) {
		Container container = new Container();
		container.setCnumber(cnumber);
		List<Container> list = containerService.queryAllByMution(container);
		JSONObject jsonObject = new JSONObject();
		if(list.size() == 1) {
			Long cid = list.get(0).getCid();
			ContainerStare containerStare = new ContainerStare();
			containerStare.setCid(cid);
			List<ContainerStare> lists = containerStareService.queryAllByMution(containerStare);
			//容器空闲或在所操作位置
			if (lists.size() == 0 || (null== lists.get(0).getWrlid() && null == ContainerTypeEnum.getNameByCode(permissionType)) || lists.get(0).getWrlid().equals(ContainerTypeEnum.getNameByCode(permissionType))) {
				jsonObject.put("status", "1");
				jsonObject.put("data", list.get(0));
				//该容器不在对应位置（不空闲且不在所操作位置）
			} else {
				jsonObject.put("status", "0");
				jsonObject.put("message", BaseAPIMessageTypeEnum.A019.getName());
			}
		}else if(list.size() > 1) {
			jsonObject.put("status","0");
			jsonObject.put("message",BaseAPIMessageTypeEnum.A002.getName());
		}else{
			jsonObject.put("status","0");
			jsonObject.put("message",BaseAPIMessageTypeEnum.A003.getName());
		}
		jsonObject = ResponseUtil.jSONObjectAdd(jsonObject,((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest());
		try {
			ResponseUtil.write(response, jsonObject);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * app喷涂装框
	 * @param json
	 * @param userId 操作人id
	 * @param permissionType 产线/库区编号
	 * @param response
	 */
	@RequestMapping("/api/boxing")
	public void boxing(String json,Long userId,String permissionType,HttpServletResponse response) {
		//获取登录人
		SysUser user = userMapper.selectById(userId);

		String code = DateUtil.dateFormat4();

		List<String> list = new ArrayList<>();

		//json转list集合
		JSONObject jsonObject = new JSONObject();
		Gson gson = new Gson();
		List<ResultAppList<LacquerInto>> resultAppList = gson.fromJson(json, new TypeToken<List<ResultAppList<LacquerInto>>>() {}.getType());
		if (resultAppList.size() > 0){
			//判断是否成功标识
			int  flg = 1;
			for (int i = 0; i <resultAppList.size() ; i++) {
				//获取对应喷涂线物料绑定信息
				List<LacquerInto> lacquerIntos = resultAppList.get(i).getList();
				//获取容器编号
				String codeNew = code + i;
				//遍历喷涂线物料绑定信息
				for (int j = 0; j < lacquerIntos.size(); j++) {
					//解绑
					LacquerInto lacquerInto = lacquerIntos.get(j);
					goodSortFrameService.untyingLacquerInto(lacquerInto);
					//绑定
					GoodContainerStare goodContainerStare = new GoodContainerStare();
					goodContainerStare.setCode(codeNew);
					goodContainerStare.setMaId(lacquerInto.getMaterielId());
					goodContainerStare.setGcNum(lacquerInto.getQuantity().intValue());
					//goodContainerStare.setColour(lacquerInto.getSprayingRequirements());
					if(null != lacquerInto.getStatus() && !"".equals(lacquerInto.getStatus())){
						goodContainerStare.setStare(lacquerInto.getStatus());
					}else{
						goodContainerStare.setStare("0");
					}
					goodContainerStare.setPsId(lacquerInto.getStationId());
					goodContainerStare.setCreatedBy(user.getUsername());
					goodContainerStare.setCreatedDate(DateUtil.dateFormat3());
					goodContainerStare.setDelFlag("0");
					Long gid = goodContainerStareService.insert(goodContainerStare);
					//判断是否绑定成功
					if (null == gid || gid <= 0){
						flg = 0;
						break;
					}
				}
				//判断是否绑定成功
				if (flg == 0){
					list.add(null);
					break;
				}else{
					list.add(codeNew);
				}
			}
			//装框成功
			if (flg > 0){
				jsonObject.put("status", "1");
				jsonObject.put("data", BaseAPIMessageTypeEnum.A004.getName());
				jsonObject.put("code", list);
			//装框失败
			}else if(flg == 0){
				jsonObject.put("status", "0");
				jsonObject.put("data", BaseAPIMessageTypeEnum.A005.getName());
			}
		}else{
			jsonObject.put("status", "0");
			jsonObject.put("message", BaseAPIMessageTypeEnum.A006.getName());
		}
		jsonObject = ResponseUtil.jSONObjectAdd(jsonObject,((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest());
		try {
			ResponseUtil.write(response, jsonObject);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * app根据编号查询成品容器绑定信息
	 * @param cnumber 框码
	 * @param type 是否入库 0否 1是
	 * @param response
	 * @return
	 */
	@RequestMapping("/api/findGoodContainerStareByCnumber")
	public void findGoodContainerStareByCnumber(String cnumber,String type,HttpServletResponse response) {
		JSONObject jsonObject = new JSONObject();
		//判断是否根据编号查询到唯一容器
		if (null != cnumber && !"".equals(cnumber.trim())) {
			//根据条码查询成品容器绑定信息
			GoodContainerStare goodContainerStare = new GoodContainerStare();
			goodContainerStare.setDelFlag("0");
			goodContainerStare.setType(type);
			goodContainerStare.setCode(cnumber.trim());
			List<GoodContainerStare> list = goodContainerStareService.queryAllByMutionGroupMaIdAndCid(goodContainerStare);
			if(list.size() > 0) {
				jsonObject.put("status", "1");
				jsonObject.put("data", list);
			}else{
				jsonObject.put("status","0");
				jsonObject.put("message", BaseAPIMessageTypeEnum.A001.getName());
			}
		}else{
			jsonObject.put("status","0");
			jsonObject.put("message",BaseAPIMessageTypeEnum.A006.getName());
		}
		jsonObject = ResponseUtil.jSONObjectAdd(jsonObject,((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest());
		try {
			ResponseUtil.write(response, jsonObject);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	/**
	 * app入库
	 * @param json
	 * @param userId 操作人id
	 * @param permissionType 产线/库区编号
	 * @param response
	 */
	@RequestMapping("/api/appGoodSortFrameOn")
	public void appGoodSortFrameOn(String json,Long userId,String permissionType,HttpServletResponse response) {
		//获取登录人
		SysUser user = userMapper.selectById(userId);
		//json转list集合
		JSONObject jsonObject = new JSONObject();
		Gson gson = new Gson();
		List<ResultAppList<GoodContainerStare>> resultAppList = gson.fromJson(json, new TypeToken<List<ResultAppList<GoodContainerStare>>>() {}.getType());
		if (resultAppList.size() > 0){
			//判断是否成功标识
			int  flg = 1;
			for (int i = 0; i <resultAppList.size() ; i++) {
				//获取库位
				String newWrlnumber = resultAppList.get(i).getNewWrlnumber();
				//获取对应喷涂线物料绑定信息
				List<GoodContainerStare> goodContainerStares = resultAppList.get(i).getList();
				//根据库位编号查询库位
				WarehousesRegionLocation warehousesRegionLocation = new WarehousesRegionLocation();
				WarehousesRegion warehousesRegion = new WarehousesRegion();
				warehousesRegion.setWrnumber(permissionType);
				warehousesRegionLocation.setWarehousesRegion(warehousesRegion);
				warehousesRegionLocation.setWrlnumber(newWrlnumber);
				List<WarehousesRegionLocation> warehousesRegionLocations = warehousesRegionLocationService.queryAllByMution(warehousesRegionLocation);
				//判断是否查询到该库位
				if(warehousesRegionLocations.size() == 1){
					//成品上架表
					GoodSortFrame goodSortFrame = new GoodSortFrame();
					goodSortFrame.setLibraryId(warehousesRegionLocations.get(0).getWrlid());
					goodSortFrame.setCreateBy(user.getUsername());
					goodSortFrame.setCreateDate(DateUtil.dateFormat3());
					goodSortFrame.setDelFlag("0");
					//遍历喷涂线物料绑定信息
					for (int j = 0; j < goodContainerStares.size(); j++) {
						int flgGid = 0;
						//根据条码和物料查询成品绑定表
						GoodContainerStare goodContainerStare = new GoodContainerStare();
						goodContainerStare.setCode(goodContainerStares.get(j).getCode());
						goodContainerStare.setMaId(goodContainerStares.get(j).getMaId());
						goodContainerStare.setDelFlag("0");
						List<GoodContainerStare> list = goodContainerStareService.queryAllByMution(goodContainerStare);
						for (int k = 0; k < list.size(); k++) {
							//绑定上架表
							goodSortFrame.setGcId(list.get(k).getGcId());
							Long gid = goodSortFrameService.insert(goodSortFrame);
							//判断是否绑定成功
							if (null == gid || gid <= 0){
								flg = 0;
								break;
							}else{
								flgGid ++;
							}
						}

						//判断是否绑定成功
						if (flgGid == 0){
							flg = 0;
							break;
						}
					}
					if (flg == 0){
						//入库失败
						jsonObject.put("status", "0");
						jsonObject.put("data", BaseAPIMessageTypeEnum.A022.getName());
						break;
					}
				}else{
					//该库位不存在或查出多条
					jsonObject.put("status", "0");
					jsonObject.put("message", BaseAPIMessageTypeEnum.A020.getName());
					flg = 0;
					break;
				}
			}
			//入库成功
			if (flg > 0){
				jsonObject.put("status", "1");
				jsonObject.put("data", BaseAPIMessageTypeEnum.A021.getName());
			}
		}else{
			jsonObject.put("status", "0");
			jsonObject.put("message", BaseAPIMessageTypeEnum.A006.getName());
		}
		jsonObject = ResponseUtil.jSONObjectAdd(jsonObject,((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest());
		try {
			ResponseUtil.write(response, jsonObject);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}


	/**
	 * app出库
	 * @param json 领料信息
	 * @param out 出库单
	 * @param userId 登录人id
	 * @param response
	 *//*
	@RequestMapping("/api/appGoodSortFrameOut")
	public void appGoodSortFrameOut(String json,String out,String userId,HttpServletResponse response) {
		//获取登录人
		SysUser user = userMapper.selectById(userId);
		//json转list集合
		JSONObject jsonObject = new JSONObject();
		Gson gson = new Gson();
		List<ReportPicking> reportPickings = gson.fromJson(json, new TypeToken<List<ReportPicking>>() {}.getType());
		OutStockOrder outStockOrder = gson.fromJson(out, new TypeToken<OutStockOrder>() {}.getType());

		if (null != reportPickings && reportPickings.size() > 0){
			int flg = 1;
			//获取某一列的值
			List<Long> idList = reportPickings.stream().map(ReportPicking -> ReportPicking.getPicId()).collect(Collectors.toList());
			String ids = StringUtil.join(idList.toArray(), ",");
			//获取某一列的值
			List<Long> numList = reportPickings.stream().map(ReportPicking -> ReportPicking.getQuantity()).collect(Collectors.toList());
			String nums = StringUtil.join(numList.toArray(), ",");
			//判断是否满足接收的领料信息返回不满足的领料信息
			List<ReportPicking> isnotReportPickings = reportPickingService.isSatisfyReportPickingsByPicIds(ids,nums);
			if(isnotReportPickings.size() > 0){
				jsonObject.put("status", "1");
				jsonObject.put("data", isnotReportPickings.get(0).getWlNumber()+":"+isnotReportPickings.get(0).getMaterial().getMaNumber()+"物料不足");
			}else{
				//新增出库单
				outStockOrder.setCreateBy(user.getUsername());
				outStockOrder.setCreateDate(DateUtil.dateFormat3());
				String outCard = "HY"+ DateUtil.dateFormat4();
				outStockOrder.setOutCard(outCard);
				Long oid = outStockOrderService.insert(outStockOrder);
				flg =reportPickingService.outReportPickingsByPicIds(ids,nums,oid);
				*//*for (int i = 0; i < reportPickings.size(); i++) {
					//领料完成状态修改
					ReportPicking reportPicking = new ReportPicking();
					reportPicking.setPicId(reportPickings.get(i).getPicId());
					reportPicking.setDelFlag("1");
					reportPickingService.update(reportPicking);
					reportPicking = reportPickings.get(i);

					OutStockOrderDetail outStockOrderDetail = new OutStockOrderDetail();
					outStockOrderDetail.setOutStockOrderId(oid);
					outStockOrderDetail.setMaId(reportPicking.getMaterielId());
					outStockOrderDetail.setRdodNum(reportPicking.getQuantity().intValue());
					outStockOrderDetailService.insert(outStockOrderDetail);

					//根据领料单查询成品链绑定信息
					*//**//*GoodSortFrame param = new GoodSortFrame();
					param.setLibraryId(reportPicking.getLocationId());
					param.setMaId(reportPicking.getMaterielId());
					param.setGcRemark("0");
					List<GoodSortFrame> list = goodSortFrameService.queryAllByMution(param);
					Long quantity = reportPicking.getQuantity();
					for (int j = 0; j < list.size(); j++) {
						//解绑
						GoodSortFrame goodSortFrame = new GoodSortFrame();
						goodSortFrame.setDelFlag("1");
						Integer count = goodSortFrameService.update(goodSortFrame);
						if(list.get(j).getMaNum().equals(quantity)){
							break;
						}else if(list.get(j).getMaNum() > quantity){
							goodSortFrame = list.get(j);
							goodSortFrame.setMaNum(goodSortFrame.getMaNum() - quantity);
							goodSortFrame.setDelFlag("0");
							goodSortFrameService.insert(goodSortFrame);
							break;
						}
						quantity = quantity - goodContainerStare.getMaNum();
						if (null == count || count <= 0){
							flg = 0;
							return;
						}
					}*//**//*

				}*//*
				if (flg > 0){
					jsonObject.put("status", "1");
					jsonObject.put("data", BaseAPIMessageTypeEnum.A017.getName());
				}else{
					jsonObject.put("status", "0");
					jsonObject.put("data", BaseAPIMessageTypeEnum.A018.getName());
				}
			}
		}else{
			jsonObject.put("status", "0");
			jsonObject.put("data", BaseAPIMessageTypeEnum.A006.getName());
		}
		jsonObject = ResponseUtil.jSONObjectAdd(jsonObject,((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest());
		try {
			ResponseUtil.write(response, jsonObject);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}*/












	/**
	 * app出库
	 * @param json
	 * @param user 操作人
	 * @param response
	 */
	@RequestMapping("/api/appGoodSortFrameOut1")
	public void appGoodSortFrameOut1(String json,String user,String out,HttpServletResponse response) {
		//判断是否成功标识
		//json转list集合
		JSONObject jsonObject = new JSONObject();
		Gson gson = new Gson();
		List<GoodContainerStare> goodContainerStares = gson.fromJson(json, new TypeToken<List<GoodContainerStare>>() {}.getType());
		OutStockOrder outStockOrder = gson.fromJson(out, new TypeToken<OutStockOrder>() {}.getType());
		if (goodContainerStares.size() > 0){
			//新增出库单
			outStockOrder.setCreateBy(user);
			outStockOrder.setCreateDate(DateUtil.dateFormat3());
			String outCard = "HY"+ DateUtil.dateFormat4();
			outStockOrder.setOutCard(outCard);
			Long oid = outStockOrderService.insert(outStockOrder);
			if(null != oid && oid > 0){
				//添加出库详情
				List<OutStockOrderDetail> outStockOrderDetails = new ArrayList<>();
				//需出库成品容器绑定id集合
				String ids = "";
				for (int i = 0; i <goodContainerStares.size() ; i++) {
					GoodContainerStare goodContainerStare = goodContainerStares.get(i);
					//添加出库单详情数据统计
					//需统计中重复list集
					int omidListi = -1;
					for (int j = 0; j < outStockOrderDetails.size(); j++) {
						if(goodContainerStare.getMaId().equals(outStockOrderDetails.get(j).getMaId())){
							omidListi = j;
							break;
						}
					}
					if(omidListi >= 0){
						outStockOrderDetails.get(omidListi).setRdodNum(outStockOrderDetails.get(omidListi).getRdodNum()+goodContainerStare.getGcNum());
					}else{
						OutStockOrderDetail outStockOrderDetail = new OutStockOrderDetail();
						outStockOrderDetail.setOutStockOrderId(oid);
						outStockOrderDetail.setMaId(goodContainerStare.getMaId());
						outStockOrderDetail.setRdodNum(goodContainerStare.getGcNum());
						outStockOrderDetails.add(outStockOrderDetail);
					}

					//解绑信息统计
					if(i == 0){
						ids = ids + goodContainerStare.getGcId();
					}else{
						ids = ids + "," + goodContainerStare.getGcId();
					}
				}
				//根据需出库成品容器绑定id集合解绑,成品上架表解绑,删除容器当前装载信息
				//Integer flgCount = goodContainerStareService.updateDelFlagByids(ids,"1");
				//出库库成功
				/*if (flgCount > 0){
					for(int i = 0; i < outStockOrderDetails.size(); i++) {
						outStockOrderDetailService.insert(outStockOrderDetails.get(i));
					}
					jsonObject.put("status", "1");
					jsonObject.put("data", BaseAPIMessageTypeEnum.A023.getName());
				}else{
					jsonObject.put("status", "0");
					jsonObject.put("message", BaseAPIMessageTypeEnum.A024.getName());
				}*/
			}else{
				jsonObject.put("status", "0");
				jsonObject.put("message", BaseAPIMessageTypeEnum.A024.getName());
			}
		}else{
			jsonObject.put("status", "0");
			jsonObject.put("message", BaseAPIMessageTypeEnum.A006.getName());
		}
		jsonObject = ResponseUtil.jSONObjectAdd(jsonObject,((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest());
		try {
			ResponseUtil.write(response, jsonObject);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}



	/**
	 * app根据编号查询待发货信息
	 * @param code 码
	 * @param response
	 * @return
	 */
	@RequestMapping("/api/findStayContainerStareByCode")
	public void findStayContainerStareByCode(String code,HttpServletResponse response) {
		JSONObject jsonObject = new JSONObject();
		StayContainerStare  stayContainerStare = new StayContainerStare();
		stayContainerStare.setCode(code);
		stayContainerStare.setRemark1("0");
		List<StayContainerStare> list = reportPickingService.queryStayContainerStareByMution(stayContainerStare);
		if(list.size() > 0) {
			jsonObject.put("status", "1");
			jsonObject.put("data", list);
		}else{
			jsonObject.put("status","0");
			jsonObject.put("message", BaseAPIMessageTypeEnum.A009.getName());
		}

		jsonObject = ResponseUtil.jSONObjectAdd(jsonObject,((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest());
		try {
			ResponseUtil.write(response, jsonObject);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}



	/**
	 * app成品链 、成品库、喷涂、待发货区出库
	 * @param json 领料信息
	 * @param out 出库单
	 * @param userId 登录人id
	 * @param code 扫码集合 用","分割
	 * @param response
	 */
	@RequestMapping("/api/appOut")
	public void appOut(String json,String out,String userId,String code,HttpServletResponse response) {
		//获取登录人
		SysUser user = userMapper.selectById(userId);
		//json转list集合
		JSONObject jsonObject = new JSONObject();
		Gson gson = new Gson();
		List<ReportPicking> reportPickings = gson.fromJson(json, new TypeToken<List<ReportPicking>>() {}.getType());
		OutStockOrder outStockOrder = gson.fromJson(out, new TypeToken<OutStockOrder>() {}.getType());

		if (null != reportPickings && reportPickings.size() > 0 && null != code && !"".equals(code)){
			int flg = 1;
			//获取某一列的值
			List<Long> idList = reportPickings.stream().map(ReportPicking -> ReportPicking.getPicId()).collect(Collectors.toList());
			String ids = StringUtil.join(idList.toArray(), ",");
			//获取某一列的值
			List<Long> numList = reportPickings.stream().map(ReportPicking -> ReportPicking.getQuantity()).collect(Collectors.toList());
			String nums = StringUtil.join(numList.toArray(), ",");
			//判断是否满足接收的领料信息返回不满足的领料信息
			List<ReportPicking> isnotReportPickings = reportPickingService.isSatisfyReportPickingsByPicIds(ids,nums,code);
			if(isnotReportPickings.size() > 0){
				jsonObject.put("status", "0");
				jsonObject.put("message", isnotReportPickings.get(0).getWlNumber()+":"+isnotReportPickings.get(0).getMaterial().getMaNumber()+"物料不足");
			}else{
				//新增出库单
				outStockOrder.setCreateBy(user.getUsername());
				outStockOrder.setCreateDate(DateUtil.dateFormat3());
				String outCard = "HY"+ DateUtil.dateFormat4();
				outStockOrder.setOutCard(outCard);
				String osoNumber = "CK"+ DateUtil.dateFormat4();
				outStockOrder.setOsoNumber(osoNumber);
				Long oid = outStockOrderService.insert(outStockOrder);
				flg =reportPickingService.outReportPickingsByPicIds(ids,nums,oid,code);
				if (flg > 0){
					jsonObject.put("status", "1");
					jsonObject.put("data", BaseAPIMessageTypeEnum.A017.getName());
				}else{
					jsonObject.put("status", "0");
					jsonObject.put("message", BaseAPIMessageTypeEnum.A018.getName());
				}
			}
		}else{
			jsonObject.put("status", "0");
			jsonObject.put("message", BaseAPIMessageTypeEnum.A006.getName());
		}
		jsonObject = ResponseUtil.jSONObjectAdd(jsonObject,((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest());
		try {
			ResponseUtil.write(response, jsonObject);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
